﻿//@ sourceURL=stockoutpos.js
/**
 * 零售小票单
 */
; (function (global, factory) {
	if (typeof module === "object" && typeof module.exports === "object") {
		module.exports = factory(global);
	} else {
		factory(global);
	}
})(typeof window !== "undefined" ? window : this, function (window) {
	function addHeaderLabel(LODOP, name, top, left, width, height) {
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left.toFixed(2) + "mm", width + "mm", height + "mm", name);
		//LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
	}
	function addHeaderValue(LODOP, value, top, left, width, height) {
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left.toFixed(2) + "mm", width + "mm", height + "mm", value);
	}
	function addLine(LODOP, top, left, width, style) {
		style = style || 1;
		//LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left + "mm", width + "mm", height + "mm", "ˉˉˉˉˉˉˉˉˉˉˉ..............ˉˉˉ.......");
		LODOP.ADD_PRINT_LINE(top.toFixed(2) + "mm", left + "mm", top + "mm", (left + width).toFixed(2) + "mm", 1, 1);//y1,x1,y2,x2,style(0:实线,1:破折线,2:点线,3:点划线,4:双点划线),width
	}

	function designAndPrintData(LODOP, config) {
		// LODOP.SET_PRINT_MODE("PRINT_NOCOLLATE", 1);
		var i;
		var top = 0;
		var left = 0;
		var titleHeight = 6.8;//标题高度
		var headerHeight = 4;//头部高度
		var tmpH, tmpW;
		var lineHeight = 1;//线高度
		var detailHeight = 6;//
		var footerHeight = 6;
		var titleSize = 18;
		var detailSize = 14;

		var titles = config.Config.title.split(' ');
		for (i = 0; i < titles.length; i++) {
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", titleHeight + "mm", titles[i]);
			LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
			LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
			LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);//1--左靠齐 2--居中 3--右靠齐
			//LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
			//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
			top += titleHeight;
		}
		// 直接在结账单下面显示单位
		if (config.Config.orgname) {
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", titleHeight + "mm", config.Config.orgname);
			LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
			LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
			LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);//1--左靠齐 2--居中 3--右靠齐
			//LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
			//LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
			top += titleHeight;
		}
		top += lineHeight;
		addLine(LODOP, top, 0, config.width);
		top += lineHeight;

		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", titleHeight + "mm", "桌号：" + (config.model.TableName || ""));
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += titleHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", headerHeight + "mm", "人数：" + (config.model.PersonCount || ""));
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += headerHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", headerHeight + "mm", "结账时间：" + config.model.AddDate);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += headerHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", headerHeight + "mm", "收银员：" + config.model.AddManName);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += headerHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", headerHeight + "mm", "单号：" + config.model.SNo);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += headerHeight;
		if (config.model.PartnerName) {
			//LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", headerHeight + "mm", "会员:" + (config.model.PartnerName.length >= 2 ? config.model.PartnerName[0] + '*' + config.model.PartnerName.substr(2) : config.model.PartnerName) + "(" + config.model.PartnerCode + ")");
			//top += headerHeight;
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", config.width + "mm", headerHeight + "mm", "会员：" + config.model.PartnerName + "(" + config.model.PartnerCode + ")");
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			top += headerHeight;
		}
		top += lineHeight;
		addLine(LODOP, top, 0, config.width);
		top += lineHeight;

		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "40mm", detailHeight + "mm", "品名");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "41mm", "15mm", detailHeight + "mm", "数量");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "48mm", "15mm", detailHeight + "mm", "单价");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", "金额");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		top += detailHeight;

		for (i = 0; i < config.model.Details.length; i++) {
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "40mm", detailHeight + "mm", config.model.Details[i].SkuName || "");
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "41mm", "15mm", detailHeight + "mm", config.model.Details[i].Amount + (config.model.Details[i].UnitMin || ""));
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "48mm", "15mm", detailHeight + "mm", config.model.Details[i].PriceVipWithTax);
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", config.model.Details[i].PriceVipWithTaxTotal);
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
			top += detailHeight;
		}
		top += lineHeight;
		addLine(LODOP, top, 0, config.width);
		top += lineHeight;

		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "商品合计");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", config.model.PriceVipWithTaxTotal);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += detailHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "权益抵扣");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", 0 - config.model.DepositTotal);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		top += detailHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "手动抹零");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", config.model.ExtraTotal);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		top += detailHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", titleHeight + "mm", "应付金额");
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", titleHeight + "mm", config.model.DuePay);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		top += titleHeight;
		top += lineHeight;
		addLine(LODOP, top, 0, config.width);
		top += lineHeight;

		var recvs = (config.model && config.model.Recvs || []);
		//for (i = 0; i < recvs.length; i++) {
		//	if (recvs[i].RecvWay == 1) {//现金付款
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", utils.formatEnum(recvs[i].RecvWay, pageEnumData.recvway));
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", recvs[i].Recved);
		//		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		//		top += detailHeight;
		//	} else if (recvs[i].RecvWay == 9) {//余额
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "会员卡支付");
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", recvs[i].Recved);
		//		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		//		top += detailHeight;
		//	} else if (recvs[i].RecvWay == 11) {//欠款
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "挂账消费");
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", recvs[i].Recved);
		//		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		//		top += detailHeight;
		//	} else {
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", utils.formatEnum(recvs[i].RecvWay, pageEnumData.recvway));
		//		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", detailHeight + "mm", recvs[i].Recved);
		//		LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		//		top += detailHeight;
		//	}
		//}
		//LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", titleHeight + "mm", "支付合计");
		//LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
		//LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "60mm", "18mm", titleHeight + "mm", config.model.Recved);
		//LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
		//LODOP.SET_PRINT_STYLEA(0, "Alignment", 3);//1--左靠齐 2--居中 3--右靠齐
		//top += titleHeight;


		var idx11 = recvs.findIndex(function (r) { return r.RecvWay == 11; });//是否挂账
		if (idx11 >= 0) {
			top += lineHeight;
			addLine(LODOP, top, 0, config.width, 3);
			top += lineHeight;
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", titleHeight + "mm", "挂账详情");
			LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
			top += titleHeight;
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "挂账金额：" + recvs[idx11].Recved);
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			top += detailHeight;
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "挂账人：" + config.model.PartnerName + "(" + config.model.PartnerPhone + ")");
			LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
			top += detailHeight;
			LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", titleHeight + "mm", "签字：");
			LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);//允许标点溢出，且英文单词拆开。
			top += titleHeight;
		}
		top += lineHeight;
		addLine(LODOP, top, 0, config.width);
		top += lineHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "50mm", detailHeight + "mm", "打印人：" + config.model.PrintManName);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += detailHeight;
		LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", "0mm", "80mm", detailHeight + "mm", "打印时间：" + config.model.PrintDate);
		LODOP.SET_PRINT_STYLEA(0, "FontSize", detailSize);
		top += detailHeight;
		//addLine(LODOP, top, 0, config.width);
		//top += lineHeight;

		//top += detailHeight;
		//var totals = [
		//	{ name: "会员优惠:", value: 0 - config.model.DiscountTotal, bold: true },
		//	{ name: "促销优惠:", value: 0 - config.model.PromotionTotal, bold: true },
		//	{ name: "额外优惠:", value: 0 - config.model.ExtraTotal, bold: true },
		//	{ name: "应收金额:", value: config.model.OrderTotal, bold: true },
		//].filter(function (t) {
		//	return !!t.value;
		//});
		//var cols = 2;
		//var j = 0;
		//left = 0;
		//tmpW = 26;
		//for (i = 0; i < totals.length; i++) {
		//	j += 1;
		//	LODOP.ADD_PRINT_TEXT(top.toFixed(2) + "mm", left.toFixed(2) + "mm", config.width + "mm", detailHeight + "mm", totals[i].name + totals[i].value);
		//	if (totals[i].bold) {
		//		LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
		//	}
		//	if (j == cols || i == totals.length - 1) {
		//		top += detailHeight;
		//		left = 0;
		//		j = 0;
		//	} else {
		//		left += tmpW;
		//	}
		//}
		if (config.Config.footer) {
			var footers = config.Config.footer.split("\n");
			for (i = 0; i < footers.length; i++) {
				LODOP.ADD_PRINT_TEXT(top + "mm", "0mm", config.width + "mm", footerHeight + "mm", footers[i]);
				top += footerHeight;
			}
		}
		if (config.Config.address) {
			LODOP.ADD_PRINT_TEXT(top + "mm", "0mm", config.width + "mm", footerHeight + "mm", "地址:" + config.Config.address);
			LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
			LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
			top += footerHeight;
		}
		if (config.Config.phone) {
			LODOP.ADD_PRINT_TEXT(top + "mm", "0mm", config.width + "mm", footerHeight + "mm", "电话:" + config.Config.phone);
			LODOP.SET_PRINT_STYLEA(0, "TextNeatRow", true);//允许标点溢出，且英文单词拆开。
			top += footerHeight;
		}
		//LODOP.ADD_PRINT_TEXT(top + "mm", "0mm", config.width + "mm", footerHeight + "mm", "\n");
	}

	function printInit(printTplConfig, tplData, action) {
		if (!printTplConfig.SvrHost) {
			printTplConfig.SvrHost = "http://localhost:8000";
		}
		jQuery.ajax({
			url: printTplConfig.SvrHost + (printTplConfig.SvrHost.substr(-1) === "/" ? "" : "/") + "CLodopfuncs.js?priority=" + new Date().getTime(),
			dataType: "script",
			success: function (resp) {
				setTimeout(function () {
					var LODOP;
					try {
						LODOP = getCLodop();
						if (!LODOP) {
							layer && layer.open({
								type: 1,
								title: false,
								closeBtn: 0,
								shadeClose: true,
								content: "<font color='#FF00FF'>Web打印服务CLodop未安装启动，点击<a href='CLodop_Setup_for_Win32NT.exe' target='_blank'>这里</a>下载安装（若已安装过，可点击<a href='CLodop.protocol:setup' target='_self'>这里</a>直接启动），成功后请刷新本页面。</font>"
							});
							return;
						}
						console.log('LODOP.CVERSION:', LODOP.CVERSION);
						if (LODOP.CVERSION < "3.0.8.3") {
							layer && layer.open({
								type: 1,
								title: false,
								closeBtn: 0,
								shadeClose: true,
								content: "<br><font color='#FF00FF'>Web打印服务CLodop需升级!点击<a href='CLodop_Setup_for_Win32NT.exe' target='_blank'>这里</a>执行升级,升级成功后请刷新本页面。</font>"
							});
						}
						//===如下空白位置适合调用统一功能(如注册语句、语言选择等):==
						LODOP.SET_LICENSES("杭州感知科技有限公司", "89600FD5AB0D66747807D05CE0721A65", "", "");
						//=======================================================
					} catch (err) {
						alert("打印控件初始化失败:" + err);
					}
					LODOP.PRINT_INIT("");//必须打印初始化一下  
					var printConfig = {};
					printConfig.width = 80;//mm
					printConfig.height = 3.5;//mm
					//注意：得力DL-5801P打印机不支持自定义纸张类型，注释掉下面一行就行
					LODOP.SET_PRINT_PAGESIZE(3, printConfig.width * 10, printConfig.height * 10, "");////设定打印页面大小:宽度固定、高度自适应
					printConfig.model = tplData;
					printConfig.Config = { orgname: globalData && globalData.user && globalData.user.OrgName || '', title: printTplConfig.Title, psize: printTplConfig.PageSize, phone: printTplConfig.Phone, address: printTplConfig.DetailAddress, footer: printTplConfig.Footer };
					var printerIndex = -1;
					if (printTplConfig.PrinterName) {
						for (var i = LODOP.GET_PRINTER_COUNT() - 1; i >= 0; i--) {
							if (LODOP.GET_PRINTER_NAME(i) == printTplConfig.PrinterName) {
								printerIndex = i;
								break;
							}
						}
					}
					LODOP.SET_PRINTER_INDEXA(printerIndex);
					designAndPrintData(LODOP, printConfig);
					switch (action) {
						case "DESIGN":
							LODOP.PRINT_DESIGN(); //打印设计
							break;
						case "SETUP":
							LODOP.PRINT_SETUP(); //打印维护
							break;
						case "PREVIEW":
							LODOP.PREVIEW(); //打印预览
							break;
						default:
							{
								LODOP.PRINT(); //直接打印
								//LODOP.SEND_PRINT_RAWDATA(String.fromCharCode(27, 112, 0, 60, 255));//打开钱箱指令未测试
								LODOP.SEND_PRINT_RAWDATA(String.fromCharCode(27, 112, 1, 128, 128));//打开钱箱
							}
							break;
					}
					// LODOP.SET_PREVIEW_WINDOW(0, 0, 0,1024 , 700, "预览查看.开始打印");
					// LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);//横向时的正向显示
					// LODOP.SET_SHOW_MODE("SETUP_ENABLESS", 11111111000001);
				}, 100);
			},
			error: function (xhr, err, ex) {
				alert("连接打印服务器失败:" + err);
			}
		});
	}

	window.stockoutpos = printInit;
});
